package com.clothe.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.clothe.dao.IUserInfoDAO;
import com.clothe.dto.UserInfo;

public class UserInfoDAOImpl implements IUserInfoDAO {

	private Connection conn = null;

	public void setConn(Connection conn) {
		this.conn = conn;
	}

	@Override
	public boolean save(UserInfo userInfo) throws Exception {
		PreparedStatement ps = null;
		boolean flag = false;
		try {
			String sql = "insert into user(user_account,user_password,user_name,address,phone) values(?,?,?,?,?)";
			ps = conn.prepareStatement(sql);
			ps.setString(1, userInfo.getUserAccount());
			ps.setString(2, userInfo.getUserPassword());
			ps.setString(3, userInfo.getUserName());
			ps.setString(4, userInfo.getAddress());
			ps.setString(5, userInfo.getPhone());
			if (ps.executeUpdate() > 0)
				flag = true;

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			ps.close();
		}
		return flag;
	}

	@Override
	public boolean delete(int id) throws Exception {
		PreparedStatement ps = null;
		boolean flag = false;
		try {
			String sql = "delete from  user where id = ?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			if (ps.executeUpdate() > 0)
				flag = true;

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			ps.close();
		}
		return flag;
	}

	@Override
	public boolean update(UserInfo userInfo) throws Exception {
		PreparedStatement ps = null;
		boolean flag = false;
		try {
			String sql = "update  user  set user_name = ?, address = ?, phone = ? where id=? ";
			ps = conn.prepareStatement(sql);
			ps.setString(1, userInfo.getUserName());
			ps.setString(2, userInfo.getAddress());
			ps.setString(3, userInfo.getPhone());
			ps.setInt(4, userInfo.getId());

			if (ps.executeUpdate() > 0)
				flag = true;

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			ps.close();
		}
		return flag;
	}

	@Override
	public UserInfo get(int id) throws Exception {
		PreparedStatement ps = null;
		ResultSet rs = null;
		UserInfo userInfo = null;

		try {
			String sql = "select * from user where id = ?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			if (rs.next()) {
				userInfo = new UserInfo();
				userInfo.setId(rs.getInt("id"));
				userInfo.setUserPassword(rs.getString("user_password"));
				userInfo.setUserAccount(rs.getString("user_account"));
				userInfo.setUserName(rs.getString("user_name"));
				userInfo.setAddress(rs.getString("address"));
				userInfo.setPhone(rs.getString("phone"));
			}

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			rs.close();
			ps.close();
		}
		return userInfo;
	}

	@Override
	public List<UserInfo> list(int pageSize, int pageNo, String queryString)
			throws Exception {
		PreparedStatement ps = null;
		ResultSet rs = null;
		UserInfo userInfo = null;
		List<UserInfo> userInfoList = null;
		try {
			userInfoList = new ArrayList<UserInfo>();
			String sql = "select * from user " + queryString + "limit ?,?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, (pageNo - 1) * pageSize);
			ps.setInt(2, pageSize);
			rs = ps.executeQuery();
			while (rs.next()) {
				userInfo = new UserInfo();
				userInfo.setId(rs.getInt("id"));
				userInfo.setUserAccount(rs.getString("user_account"));
				userInfo.setUserName(rs.getString("user_name"));
				userInfo.setAddress(rs.getString("address"));
				userInfo.setPhone(rs.getString("phone"));
				userInfoList.add(userInfo);
			}

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			rs.close();
			ps.close();
		}
		return userInfoList;
	}

	@Override
	public UserInfo login(String account, String password) throws Exception {
		PreparedStatement ps = null;
		ResultSet rs = null;
		UserInfo userinfo = null;
		try {
			String sql = "select * from user where user_account = ? and user_password = ?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, account);
			ps.setString(2, password);
			rs = ps.executeQuery();
			if (rs.next()) {
				userinfo = new UserInfo();
				userinfo.setId(rs.getInt("id"));
				userinfo.setUserName(rs.getString("user_name"));
			}

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			rs.close();
			ps.close();

		}
		return userinfo;
	}

	@Override
	public int getCount(String queryString) throws Exception {
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			String sql = "select count(*) from user " + queryString;
			ps = conn.prepareStatement(sql);
			ps.executeQuery();
			rs = ps.executeQuery();
			if (rs.next())
				return rs.getInt(1);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

			rs.close();
			ps.close();
		}
		return 0;

	}

	@Override
	public boolean updatePsw(String newpsw,UserInfo userInfo) throws Exception {

		PreparedStatement ps = null;
		boolean flag = false;
		try {
			String sql = "update  user  set user_password = ? where id=? ";
			ps = conn.prepareStatement(sql);
			ps.setString(1, newpsw);
			ps.setInt(2, userInfo.getId());
			if (ps.executeUpdate() > 0)
				flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ps.close();
		}
		return flag;

	}
}
